//AMLOW: delete this old code if not useful..
//package model.trader.portfolio;
//
//import java.util.List;
//
//import model.market.MarketInformation;
//import model.market.MarketManager;
//import model.market.Position;
//import model.stats.RegressionCalc.RegressionResult;
//import model.trader.TradeInstruction;
//
////AMXX: want to kind of include leveraging somehow.
////AMXX: hate maths with doubles and NaNs and all that. change it.
////AMXX: the single index model ignores market cap weightings? not good for managing benchmark risk eh.
//public class SingleIndexModelPortfolioManager implements PortfolioManagementStrategy{
//
//	public TradeInstruction[] allocate(Position pos, MarketManager market,
//			double[] analystPrices,
//			MarketInformation marketInfo) {
//		//Need:
//		double[] alpha = new double[analystPrices.length];
//		//here we assume that the difference between the analyst price and the fair price will produce
//		// the risk free rate. Then we calculate that cashflow, divide it by the market price to find the alpha.
//		for(int i=0;i<analystPrices.length;i++){
//			double difference = analystPrices[i]-market.getMarketPrice(i);
//			double rfCashflow = difference*market.getConfig().getRiskFreeRate();
//			alpha[i]=rfCashflow/market.getMarketPrice(i);
//			if(i==0){
//				System.out.println("We got an alpha of: "+alpha[i]);
//			}
//		}
//		
//		//variance of returns on market index.
//		Double marketVariance = marketInfo.getIndexVariance().get(marketInfo.getIndexVariance().size()-1);
//		System.out.println("Market variance: "+marketVariance );
//		
//		//	betas for stocks;
//		double[] betas = new double[marketInfo.getNumberOfAssets()];
//		
//		//variance for stocks;
//		double[] assetNonSysVariance = new double[marketInfo.getNumberOfAssets()];
//		
//		for(int i=0;i<marketInfo.getNumberOfAssets();i++){
//			//betas..
//			List<RegressionResult> assetFigures = marketInfo.getAssetCapmResults()[i];
//			RegressionResult latest = assetFigures.get(assetFigures.size()-1);
//			betas[i]=latest.getBeta();
//			assetNonSysVariance[i]=marketInfo.getAssetNonSpecificVariance(i);
//		}
//		
//		//	return for market index;
//		List<Double> marketExcessReturns = marketInfo.getIndexReturnsInclCashAndCapitalLessRiskFree();
//		//AMXX: arithmetic average is ok right?
//		//AMXX: restrict sample set for this too?
//		double returnSum = 0;
//		for(int i=0;i<marketExcessReturns.size();i++){
//			returnSum+=marketExcessReturns.get(i);
//		}
//		double marketExcessReturn = returnSum/marketExcessReturns.size();
//		
//		double[] alphasOverVariances = new double[alpha.length];
//		double sumOfAlphasOverVariances = 0;
//		//AM: risk return calcs..
//		for(int i=0;i<alpha.length;i++){
//			alphasOverVariances[i]=alpha[i]/assetNonSysVariance[i];
//			sumOfAlphasOverVariances+=Math.abs(alphasOverVariances[i]);//AMXX: temp the math.abs thing.. i just dont know how to handle negatives properly.
//		}
//		
//		double[] assetWeightsInActivePortfolio = new double[alpha.length];
//		double activePortfolioVariance =0;
//		double activePortfolioReturn = 0;
//		double activePortfolioBeta = 0;
//		//AM: weight calcs..
//		for(int i=0;i<alphasOverVariances.length;i++){
//			double value = isZero(sumOfAlphasOverVariances)?0:
//				alphasOverVariances[i]/sumOfAlphasOverVariances;
//			assetWeightsInActivePortfolio[i]=value;
//			activePortfolioVariance += assetNonSysVariance[i]
//			                                 *assetWeightsInActivePortfolio[i]
//			                                 *assetWeightsInActivePortfolio[i];
//			activePortfolioReturn += alpha[i]*assetWeightsInActivePortfolio[i];
//			activePortfolioBeta += betas[i]*assetWeightsInActivePortfolio[i];
//		}
//		
//		double activePortfolioWeightUnadjusted = isZero(marketVariance)||isZero(activePortfolioVariance)?0:
//								(activePortfolioReturn/activePortfolioVariance)/
//								(marketExcessReturn/marketVariance);
////		System.out.println("Active unadjusted: "+activePortfolioWeightUnadjusted+" activeProtfolioret "+activePortfolioReturn+
////						" apb "+activePortfolioBeta+" marketexcess "+marketExcessReturn+" marketVariance "+marketVariance+
////						" apv "+activePortfolioVariance);
//		
//		double activePortfolioWeight = activePortfolioWeightUnadjusted/
//							(1+(1-activePortfolioBeta)*activePortfolioWeightUnadjusted);
//		double indexPortfolioWeight = (1-activePortfolioWeight);
//		System.out.println("active portfolio weight: "+activePortfolioWeight+" index portfolio weight "+indexPortfolioWeight);
//		double indexPrice = marketInfo.getIndexPrices().get(marketInfo.getIndexPrices().size()-1);
//		
//		TradeInstruction[] trades = new TradeInstruction[marketInfo.getNumberOfAssets()];
//		//AM: determine actual asset allocations and create trade instructions..
//		for(int i=0;i<marketInfo.getNumberOfAssets();i++){
//			//AM: dodgy market cap calc.
//			double desiredSumWeight = (activePortfolioWeight*assetWeightsInActivePortfolio[i]+
//										indexPortfolioWeight*market.getMarketPrice(i)/(indexPrice));
//			if(desiredSumWeight<0){
//				//AMXX: check no weights are too high either.
//				//AMXX: temp..throw new RuntimeException("We have a negative weighting in a stock. Figure out how to handle this case.");
//				//AMXX: extremely temp
//				//System.out.println("we wanted  "+desiredSumWeight+" in "+i);
//				//desiredSumWeight=0;
//			}
//			
//			double desiredHolding = desiredSumWeight*pos.getWorth(market.getMarketPrices());
//			
////			System.out.println("Alpha "+alpha[i]+" beta "+betas[i]+" desiredSumWeight "+desiredSumWeight+
////					" activePortWeight "+activePortfolioWeight+" indexportweight "+indexPortfolioWeight+
////					" indexPrice "+indexPrice+" assetWeightIn "+assetWeightsInActivePortfolio[i]);
//			
//			double currentHolding = pos.getExecutedQuantity(i)*market.getMarketPrice(i);
//			System.out.println("Current holding is "+currentHolding+" in "+i);
//			int numberToTrade = (int)((desiredHolding-currentHolding)/market.getMarketPrice(i));
//			TradeInstruction instruction = new TradeInstruction();
//			instruction.setAsset(i);
//			instruction.setNumberToTrade(numberToTrade);
//			//AMXX: the hold price..
////			if(instruction.getNumberToTrade()>0){
////				instruction.setHoldPrice(market.getMarketPrice(i)*1.05);
////			}else{
////				instruction.setHoldPrice(market.getMarketPrice(i)/1.05);
////			}
//			trades[i]=instruction;
//		}
//		
//		return trades;
//	}
//
//	private boolean isZero(double number) {
//		//AMXX: : dodgy, fix..
//		return Double.isNaN(number)||(number<.0000001&&number>-.0000001);
//	}
//
//}
